//
//  SASaleDocumentsDetailCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/5/11.
//

#import "SASaleDocumentsDetailCell.h"
#import "SASalesInvoiceDetailModel.h"

@interface SASaleDocumentsDetailCell ()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *colourSizeLab;
@property (nonatomic, strong) UILabel *numLab;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UILabel *discountPriceLab;
@property (nonatomic, strong) UILabel *percentageLab;
@property (nonatomic, strong) UILabel *totalPriceLab;
@property (nonatomic, strong) UILabel *returnLab;
@property (nonatomic, strong) UIImageView *giveImageV;

@end

@implementation SASaleDocumentsDetailCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASaleSkuListModel *)model {
    _model = model;
    self.giveImageV.hidden = YES;
    self.titleLab.text = [NSString stringWithFormat:@"%@,%@",model.colourName,model.sizeName];
    self.numLab.text = model.count;
    self.priceLab.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]/100.f];
    CGFloat discountRate = [model.discountRate floatValue]/100;
    if (discountRate == 100) {
        self.totalPriceLab.frame = SARectMake(236, 0, 65, 40);
        self.percentageLab.text = @"";
        NSString *textContent = [NSString stringWithFormat:@"%0.2f",[model.totalPrice floatValue]/100.f];;
        self.totalPriceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:nil];
    }else {
        self.percentageLab.text = [NSString stringWithFormat:@"%0.2f%%",discountRate];
        self.totalPriceLab.frame = SARectMake(236, 20, 65, 15);
        NSString *textContent = [NSString stringWithFormat:@"%0.2f",[model.totalPrice floatValue]/100.f];;
        NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
        textDict[NSStrikethroughColorAttributeName] = kPinkColor;
        textDict[NSForegroundColorAttributeName] = kBlackColor;
        textDict[NSFontAttributeName] = kFont(13);
        textDict[NSStrikethroughStyleAttributeName] = [NSNumber numberWithInteger:NSUnderlineStyleSingle];
        textDict[NSBaselineOffsetAttributeName] = @(NSUnderlineStyleSingle);
        self.totalPriceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:textDict];
        if ([model.giveFlag isEqualToString:@"1"]) {
            self.percentageLab.text = @"赠送";
            self.giveImageV.hidden = NO;
        }
    }
    
    self.discountPriceLab.text = [NSString stringWithFormat:@"%0.2f",[model.discountPrice floatValue]/100.f];

    NSInteger returnCount = [model.returnCount integerValue];
    NSInteger count = [model.count integerValue];
    if (model.salesCount.length > 0) {
        count = [model.salesCount integerValue];
    }
    if (returnCount > 0) {
        self.returnLab.hidden = NO;
        self.discountPriceLab.frame = SARectMake(296, 5, 78, 15);
        if (returnCount < count) {
            self.returnLab.text = @"部分退";
        }else {
            self.returnLab.text = @"已退款";
        }
    }else {
        self.returnLab.hidden = YES;
        self.discountPriceLab.frame = SARectMake(296, 0, 78, 40);
    }
}

-(void)initUI {
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.colourSizeLab];
    [self.contentView addSubview:self.numLab];
    [self.contentView addSubview:self.priceLab];
    [self.contentView addSubview:self.discountPriceLab];
    [self.contentView addSubview:self.percentageLab];
    [self.contentView addSubview:self.totalPriceLab];
    [self.contentView addSubview:self.returnLab];
    [self.contentView addSubview:self.giveImageV];
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(20, 0, 60, 40)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(13);
    }
    return _titleLab;
}

-(UILabel *)numLab {
    if (!_numLab) {
        _numLab = [[UILabel alloc] initWithFrame:SARectMake(101, 0, 65, 40)];
        _numLab.textColor = kBlackColor;
        _numLab.font = kFont(13);
        _numLab.textAlignment = NSTextAlignmentCenter;
    }
    return _numLab;
}

-(UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:SARectMake(179, 0, 65, 40)];
        _priceLab.textColor = kBlackColor;
        _priceLab.font = kFont(13);
        _priceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _priceLab;
}


-(UILabel *)percentageLab {
    if (!_percentageLab) {
        _percentageLab = [[UILabel alloc] initWithFrame:SARectMake(236, 5, 65, 15)];
        _percentageLab.textColor = kPinkColor;
        _percentageLab.font = kFont(13);
        _percentageLab.textAlignment = NSTextAlignmentCenter;
    }
    return _percentageLab;
}

-(UILabel *)totalPriceLab {
    if (!_totalPriceLab) {
        _totalPriceLab = [[UILabel alloc] initWithFrame:SARectMake(236, 0, 65, 40)];
        _totalPriceLab.textColor = kBlackColor;
        _totalPriceLab.font = kFont(13);
        _totalPriceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _totalPriceLab;
}

-(UILabel *)discountPriceLab {
    if (!_discountPriceLab) {
        _discountPriceLab = [[UILabel alloc] initWithFrame:SARectMake(296, 0, 78, 40)];
        _discountPriceLab.textColor = kBlackColor;
        _discountPriceLab.font = kFont(13);
        _discountPriceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _discountPriceLab;
}


-(UILabel *)returnLab {
    if (!_returnLab) {
        _returnLab = [[UILabel alloc] initWithFrame:SARectMake(296, 20, 78, 15)];
        _returnLab.textColor = kPinkColor;
        _returnLab.font = kFont(11);
        _returnLab.textAlignment = NSTextAlignmentCenter;
        _returnLab.text = @"已退款";
    }
    return _returnLab;
}

-(UIImageView *)giveImageV {
    if (!_giveImageV) {
        _giveImageV = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - kScaleNum(30), 0, kScaleNum(30), kScaleNum(30))];
        _giveImageV.image = [UIImage imageNamed:@"shop_give"];
    }
    return _giveImageV;
}

@end
